package cn.com.shopec.erp.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.ILogisticsSettleDao;
import cn.com.shopec.erp.base.entity.LogisticsSettle;
import cn.com.shopec.erp.base.service.ILogisticSettleService;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.utils.LogicUtil;

@Service
public class LogisticSettleServiceImpl implements ILogisticSettleService{
	
	@Resource
	private ILogisticsSettleDao logSettleDao;

	@Override
	@Transactional
	public LogisticsSettle setLogisticsSettleInfo(LogisticsSettle logisticsSettle) throws Exception {
		List<LogisticsSettle> setList=getLogisticsSettleByCode(logisticsSettle.getCode(),logisticsSettle.getSettleType());
		LogisticsSettle set=null;
		if(setList.size()==0){
			set= new LogisticsSettle();
			set.setCode(logisticsSettle.getCode());
			set.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);//删除标识
			set.setSettleinfoCode(SZSCMConstant.genUUID());
			set.setPoundage(logisticsSettle.getPoundage());//手续费
			set.setSettleTime(logisticsSettle.getSettleTime());
			set.setSettleType(logisticsSettle.getSettleType());
			return logSettleDao.save(set);
		}else{
			 set = setList.get(0);
			set.setCode(logisticsSettle.getCode());
			set.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);//删除标识
			set.setSettleinfoCode(logisticsSettle.getSettleinfoCode());
			set.setPoundage(logisticsSettle.getPoundage());//手续费
			set.setSettleTime(logisticsSettle.getSettleTime());
			set.setSettleType(logisticsSettle.getSettleType());
			return logSettleDao.update(set);
		}
	}

	@Override
	public List<LogisticsSettle> getLogisticsSettleByCode(String code,String settleType) {
		DetachedCriteria detachedCriteria = logSettleDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("deleteFlag",SZSCMConstant.DELETE_FLAG_NO));
		if(null != code && LogicUtil.isNotNullAndEmpty(code)){
			detachedCriteria.add(Restrictions.eq("code", code));
		}
		if(null !=settleType && LogicUtil.isNotNullAndEmpty(settleType) ){
			detachedCriteria.add(Restrictions.eq("settleType", settleType));
		}
		return logSettleDao.findByCriteria(detachedCriteria);
	}

	@Override
	public List<LogisticsSettle> getAllLogisticsSettleByCode(String code) {
		DetachedCriteria detachedCriteria = logSettleDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("code", code));
		detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		return logSettleDao.findByCriteria(detachedCriteria);
	}

	
	
}
